/*******************************************************************************
 * File:      list.h                                                           *
 * Function:  implementation of double link list                               *                 *
 * Author:    kvmaker                                                          *
 * Email:     kvmaker@gmail.com                                                *
 * Data:      24/Feb/2012                                                      *
 *                                                                             *
 * Copyright: kvmaker                                                          *
 *                                                                             *
 * Licensed under the Apache License, Version 2.0 (the "License");             *
 * you may not use this file except in compliance with the License.            *
 * You may obtain a copy of the License at                                     *
 *                                                                             *
 *    http://www.apache.org/licenses/LICENSE-2.0                               *
 *                                                                             *
 * Unless required by applicable law or agreed to in writing, software         *
 * distributed under the License is distributed on an "AS IS" BASIS,           *
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.    *
 * See the License for the specific language governing permissions and         *
 * limitations under the License.                                              *
 ******************************************************************************/
#ifndef __LIST_H__
#define __LIST_H__

#include "comdef.h"

#define LIST_POISON1  ((void *) 0x00100100)
#define LIST_POISON2  ((void *) 0x00200200)

typedef struct _LIST_HEAD 
{
	struct _LIST_HEAD *next;
	struct _LIST_HEAD *prev;
} LIST_HEAD;

static inline void INIT_LIST_HEAD(LIST_HEAD *list)
{
	list->next = list;
	list->prev = list;
}

static inline void __list_add(LIST_HEAD *new,
            			      LIST_HEAD *prev,
            			      LIST_HEAD *next)
{
	next->prev = new;
	new->next = next;
	new->prev = prev;
	prev->next = new;
}

static inline void list_add(LIST_HEAD *new, LIST_HEAD *head)
{
	__list_add(new, head, head->next);
}

static inline void list_add_tail(LIST_HEAD *new, LIST_HEAD *head)
{
	__list_add(new, head->prev, head);
}

static inline void __list_del(LIST_HEAD * prev, LIST_HEAD * next)
{
	next->prev = prev;
	prev->next = next;
}

static inline void list_del(LIST_HEAD *entry)
{
	__list_del(entry->prev, entry->next);
	entry->next = LIST_POISON1;
	entry->prev = LIST_POISON2;
}

static inline int list_empty(const LIST_HEAD *head)
{
	return head->next == head;
}

#define list_entry(ptr, type, member) container_of(ptr, type, member)

#define list_for_each_entry(pos, head, member)				            \
	for (pos = list_entry((head)->next, typeof(*pos), member);	        \
	     &pos->member != (head); 	                                    \
	     pos = list_entry(pos->member.next, typeof(*pos), member))

#define list_for_each_entry_safe(pos, n, head, member)			        \
        for (pos = list_entry((head)->next, typeof(*pos), member),      \
            n = list_entry(pos->member.next, typeof(*pos), member);     \
             &pos->member != (head);                                    \
             pos = n, n = list_entry(n->member.next, typeof(*n), member))


#endif /*__LIST_H__*/

